package cn.org.seventh.web.common;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.context.Theme;
import org.springframework.ui.context.ThemeSource;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver;

/**
 * 增强的spring content获取工具 仅用于web层获取content
 * 
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since 2008-6-22 上午03:32:07
 */
public abstract class SpringContextUtils {

	/**
	 * Look for the WebApplicationContext associated with the DispatcherServlet
	 * that has initiated request processing.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the request-specific web application context
	 * @throws IllegalStateException
	 *             if no servlet-specific context has been found
	 */
	public static WebApplicationContext getWebApplicationContext(
			ServletRequest request) throws IllegalStateException {
		return SpringContextUtils.getWebApplicationContext(request, null);
	}

	/**
	 * Look for the WebApplicationContext associated with the DispatcherServlet
	 * that has initiated request processing, and for the global context if none
	 * was found associated with the current request. This method is useful to
	 * allow components outside the framework, such as JSP tag handlers, to
	 * access the most specific application context available.
	 * 
	 * @param request
	 *            current HTTP request
	 * @param servletContext
	 *            current servlet context
	 * @return the request-specific WebApplicationContext, or the global one if
	 *         no request-specific context has been found
	 * @throws IllegalStateException
	 *             if neither a servlet-specific nor a global context has been
	 *             found
	 */
	public static WebApplicationContext getWebApplicationContext(
			ServletRequest request, ServletContext servletContext)
			throws IllegalStateException {
		WebApplicationContext webApplicationContext = (WebApplicationContext) request
				.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (webApplicationContext == null) {
			if (servletContext == null) {
				throw new IllegalStateException(
						"No WebApplicationContext found: not in a DispatcherServlet request?");
			}
			webApplicationContext = WebApplicationContextUtils
					.getWebApplicationContext(servletContext);
			if (webApplicationContext == null) {
				throw new IllegalStateException(
						"No WebApplicationContext found: no ContextLoaderListener registered?");
			}
		}
		return webApplicationContext;
	}

	/**
	 * 从多个环境中加载spring bean 先从request中取 再从绑定到servlet命中 最后取context中的root
	 * 
	 * @param request
	 * @param dispatcherServletName
	 * @param servletContext
	 * @return
	 */
	public static WebApplicationContext getWebApplicationContext(
			ServletRequest request, String dispatcherServletName,
			ServletContext servletContext) {
		WebApplicationContext webApplicationContext = (WebApplicationContext) request
				.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (null != webApplicationContext) {
			return webApplicationContext;
		}
		webApplicationContext = (WebApplicationContext) servletContext
				.getAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX
						+ dispatcherServletName);
		if (null != webApplicationContext) {
			return webApplicationContext;
		}
		return WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
	}

	/**
	 * Return the LocaleResolver that has been bound to the request by the
	 * DispatcherServlet.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the current LocaleResolver, or <code>null</code> if not found
	 */
	public static LocaleResolver getLocaleResolver(HttpServletRequest request) {
		return (LocaleResolver) request
				.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
	}

	/**
	 * Retrieves the current locale from the given request, using the
	 * LocaleResolver bound to the request by the DispatcherServlet (if
	 * available), falling back to the request's accept-header Locale.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the current locale, either from the LocaleResolver or from the
	 *         plain request
	 * @see #getLocaleResolver
	 * @see javax.servlet.http.HttpServletRequest#getLocale()
	 */
	public static Locale getLocale(HttpServletRequest request) {
		LocaleResolver localeResolver = SpringContextUtils
				.getLocaleResolver(request);
		if (localeResolver != null) {
			return localeResolver.resolveLocale(request);
		} else {
			return request.getLocale();
		}
	}

	/**
	 * Return the ThemeResolver that has been bound to the request by the
	 * DispatcherServlet.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the current ThemeResolver, or <code>null</code> if not found
	 */
	public static ThemeResolver getThemeResolver(HttpServletRequest request) {
		return (ThemeResolver) request
				.getAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE);
	}

	/**
	 * Return the ThemeSource that has been bound to the request by the
	 * DispatcherServlet.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the current ThemeSource
	 */
	public static ThemeSource getThemeSource(HttpServletRequest request) {
		return (ThemeSource) request
				.getAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE);
	}

	/**
	 * Retrieves the current theme from the given request, using the
	 * ThemeResolver and ThemeSource bound to the request by the
	 * DispatcherServlet.
	 * 
	 * @param request
	 *            current HTTP request
	 * @return the current theme, or <code>null</code> if not found
	 * @see #getThemeResolver
	 */
	public static Theme getTheme(HttpServletRequest request) {
		ThemeResolver themeResolver = SpringContextUtils
				.getThemeResolver(request);
		ThemeSource themeSource = SpringContextUtils.getThemeSource(request);
		if (themeResolver != null && themeSource != null) {
			String themeName = themeResolver.resolveThemeName(request);
			return themeSource.getTheme(themeName);
		} else {
			return null;
		}
	}

	/**
	 * Find the root WebApplicationContext for this web application, which is
	 * typically loaded via ContextLoaderListener or ContextLoaderServlet.
	 * <p>
	 * Will rethrow an exception that happened on root context startup, to
	 * differentiate between a failed context startup and no context at all.
	 * 
	 * @param sc
	 *            ServletContext to find the web application context for
	 * @return the root WebApplicationContext for this web app, or
	 *         <code>null</code> if none
	 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
	 */
	public static WebApplicationContext getWebApplicationContext(
			ServletContext sc) {
		Assert.notNull(sc, "ServletContext must not be null");
		Object attr = sc
				.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (attr == null) {
			return null;
		}
		if (attr instanceof RuntimeException) {
			throw (RuntimeException) attr;
		}
		if (attr instanceof Error) {
			throw (Error) attr;
		}
		if (!(attr instanceof WebApplicationContext)) {
			throw new IllegalStateException(
					"Root context attribute is not of type WebApplicationContext: "
							+ attr);
		}
		return (WebApplicationContext) attr;
	}

	/**
	 * Find the root WebApplicationContext for this web application, which is
	 * typically loaded via ContextLoaderListener or ContextLoaderServlet.
	 * <p>
	 * Will rethrow an exception that happened on root context startup, to
	 * differentiate between a failed context startup and no context at all.
	 * 
	 * @param sc
	 *            ServletContext to find the web application context for
	 * @return the root WebApplicationContext for this web app
	 * @throws IllegalStateException
	 *             if the root WebApplicationContext could not be found
	 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
	 */
	public static WebApplicationContext getRequiredWebApplicationContext(
			ServletContext sc) throws IllegalStateException {
		WebApplicationContext wac = SpringContextUtils
				.getWebApplicationContext(sc);
		if (wac == null) {
			throw new IllegalStateException(
					"No WebApplicationContext found: no ContextLoaderListener registered?");
		}
		return wac;
	}
}
